//
//  HMAccountViewModel.m
//  weibo
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMAccountViewModel.h"
#import "HMSessionManger.h"
#define PATH [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"accountModel.plist"]
@interface HMAccountViewModel ()

@property(nonatomic,assign)BOOL isexpres;

@end

@implementation HMAccountViewModel
-(BOOL)isexpres{
    if ([self.accountModel.expires_date compare:[NSDate date]] == NSOrderedDescending) {
        return YES;
    }
    return NO;
}

-(BOOL)isSucceedLogion{
    if (self.accountModel.access_token != nil && self.isexpres) {
        return YES;
    }
    return NO;
}


+(instancetype)shared{
    static HMAccountViewModel *hmaccountModel ;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hmaccountModel = [[self alloc]init];
    });
    return hmaccountModel;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.accountModel = [self loadUserAccounthmaccountModel];
    }
    return self;
}

-(void)loadAccesstokenCode:(NSString *)codestr isSucceedBlock:(void(^)(BOOL isSucceed))isSucceedBlock{
    NSString *urlstring = @"https://api.weibo.com/oauth2/access_token";
    NSDictionary *parameters = @{
                                 @"client_id" : client_id,
                                 @"client_secret" : client_secret,
                                 @"grant_type" : @"authorization_code",
                                 @"code" : codestr,
                                 @"redirect_uri" : redirect_uri
                                 };
 [[HMSessionManger shared]requestMethod:POST urlString:urlstring parameters:parameters finishedBlock:^(NSDictionary* finished, NSError *error) {
     
     if (error != nil) {
         isSucceedBlock(NO);
         return;
     }
//     NSLog(@"%@",finished);
     
     [self loadUserMessageDic:finished isSucceedBlock:^(BOOL isSucceed) {
         isSucceedBlock(isSucceed);
     }];
     
 }];
    
}

-(void)loadUserMessageDic:(NSDictionary*)dic isSucceedBlock:(void(^)(BOOL isSucceed))isSucceedBlock{
    NSString *access_token = dic[@"access_token"];
    NSString *uid = dic[@"uid"];
    NSString *urlstring = @"https://api.weibo.com/2/users/show.json";
    NSDictionary *paramater = @{
                                @"access_token" : access_token,
                                @"uid" : uid
                                };
    [[HMSessionManger shared]requestMethod:GET urlString:urlstring parameters:paramater finishedBlock:^(id finished, NSError *error) {
     
        if (error != nil) {
            isSucceedBlock(NO);
            return ;
        }
        
//        NSLog(@"%@",finished);
        NSMutableDictionary *midc = [NSMutableDictionary dictionaryWithDictionary:finished];
        [midc setValuesForKeysWithDictionary:dic];
        NSLog(@"%@",midc);
        //字典转模型
        HMAccountModel *model = [HMAccountModel yy_modelWithDictionary:midc];
        NSLog(@"%@",model.access_token);
        [self saveUserAccounthmaccountModel:model];
        
        #pragma mark accountModel赋值
        self.accountModel = model;
        isSucceedBlock(YES);
    }];
}

#pragma mark
#pragma mark - 归档  接档
-(void)saveUserAccounthmaccountModel:(HMAccountModel*)hmaccountModel{
    [NSKeyedArchiver archiveRootObject:hmaccountModel toFile:PATH];
    NSLog(@"%@",PATH);
}
-(HMAccountModel*)loadUserAccounthmaccountModel{
   return [NSKeyedUnarchiver unarchiveObjectWithFile:PATH];
}


@end
